const db = require('./config/database')

async function testAPI () {
  try {
    console.log('测试数据库连接和数据...')

    // 测试数据库连接
    const testQuery = await db.query('SELECT 1 as test')
    console.log('✅ 数据库连接正常')

    // 检查表是否存在
    const tables = await db.query(`
      SHOW TABLES LIKE 'novels'
    `)
    console.log('novels表存在:', tables.length > 0)

    // 检查novels表数据
    const novels = await db.query('SELECT * FROM novels LIMIT 5')
    console.log('小说数据:', novels.length, '条')
    novels.forEach(novel => {
      console.log(`- ${novel.title} (作者ID: ${novel.author_id})`)
    })

    // 检查authors表数据
    const authors = await db.query('SELECT * FROM authors LIMIT 5')
    console.log('作者数据:', authors.length, '条')
    authors.forEach(author => {
      console.log(`- ${author.name}: ${author.bio}`)
    })

    // 检查chapters表数据
    const chapters = await db.query('SELECT * FROM chapters LIMIT 5')
    console.log('章节数据:', chapters.length, '条')
    chapters.forEach(chapter => {
      console.log(`- ${chapter.title} (小说ID: ${chapter.novel_id})`)
    })
  } catch (error) {
    console.error('测试失败:', error)
  }
}

testAPI()
